package riteh.eventNetwork.client;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ActivityNotifications extends ListActivity {
	
	private ListView list;
	private NotificationsAdapter adapter;
	private long userId;
	private String notifications[][];
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(C.TAG, "NOTIFICATIONS Activity");
		setContentView(R.layout.notifications);
		
	// INSTANCIRANJE OBJEKTA
		list = getListView();
		
	// AKCIJE NA STARTU
		userId = getIntent().getExtras().getLong("userId");
		
		getNotifications();
	}
	
	private void getNotifications() {
    	Log.d(C.TAG, "NOTIFICATIONS: getNotifications()");
    	
    	RestClient client = new RestClient(getString(R.string.serverMethodGetNotifications));
    	client.AddParam("userId", String.valueOf(userId));
    	
    	String result[][] = C.getMethod2(client, getString(R.string.serverMethodGetNotifications));
    	
    	if (C.resultSuccess(this, result, getString(R.string.toastGetNotificationsError))) {

    		notifications = new String[result.length][2];
    		notifications = result;
    		
			adapter = new NotificationsAdapter(this, notifications);
			list.setAdapter(adapter);
    	}
	}
	

	private class NotificationsAdapter extends ArrayAdapter<String[]> {
		private final Activity context;
		private final String[][] notifications;

		public NotificationsAdapter(Activity context, String[][] notifications) {
			super(context, R.layout.home_list, notifications);
			this.context = context;
			this.notifications = notifications;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = context.getLayoutInflater();
			View rowView = inflater.inflate(R.layout.notifications_list, null, true);
			
			TextView text = (TextView) rowView.findViewById(R.id.notificationsListLabelText);
			TextView date = (TextView) rowView.findViewById(R.id.notificationsListLabelDate);

			text.setText(notifications[position][0]);
			date.setText(notifications[position][1]);

			return rowView;
		}
	}
}
